﻿/*

给定两个字符串形式的非负整数 num1 和num2 ，计算它们的和。
 
提示：
 
1. num1 和num2 的长度都小于 5100
2. num1 和num2 都只包含数字 0-9
3. num1 和num2 都不包含任何前导零
4. 你不能使用任何內建 BigInteger 库， 也不能直接将输入的字符串转换为整数形式
*/

#include <string>
#include "gmock/gmock.h"

using std::string; 
using namespace testing;


class AddString_Solution {
public:
	string operator()(string lstrnu, string rstrnu) {
		string result{};
	
		auto liter = lstrnu.rbegin();   
		auto riter = rstrnu.rbegin();

		bool cin = false;
		while (liter != lstrnu.rend() || riter != rstrnu.rend())
		{
			char lnu = liter != lstrnu.rend() ? *liter++ : '0';
			char rnu = riter != rstrnu.rend() ? *riter++ : '0';

			char res =  cin ? lnu + rnu - '0' + 1 : lnu + rnu - '0';

			cin = res > '9';
			res = res > '9' ? res - 10 : res;

			result += res;
		}

		if (cin)
		{
			result += '1';	
		}

		result = string(result.rbegin(), result.rend());
		return result;
	}
};

class AddString_Test : public ::testing::Test {
public:
	AddString_Solution sln;
};

TEST_F(AddString_Test, zero) {
	auto lnu = "0";
	auto rnu = "0";
	
	ASSERT_THAT(sln("0", "0"), Eq(string("0")));
	ASSERT_THAT(sln("0", "1"), Eq(string("1")));
	ASSERT_THAT(sln("1", "9"), Eq(string("10")));
	ASSERT_THAT(sln("19", "9"), Eq(string("28")));
	ASSERT_THAT(sln("99", "9"), Eq(string("108")));
	ASSERT_THAT(sln("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", 
		"1"), Eq(string("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")));
		
}